package com.xuecheng.content.controller;

import com.xuecheng.api.content.TeachPlanApi;
import com.xuecheng.api.content.model.dto.TeachplanDTO;
import com.xuecheng.api.content.model.vo.TeachplanVO;
import com.xuecheng.common.util.SecurityUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.service.TeachplanService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程计划 前端控制器
 * </p>
 *
 * @author itcast
 */
@Slf4j
@RestController
@RequestMapping("teachplan")
public class TeachplanController implements TeachPlanApi {

    @Autowired
    private TeachplanService  teachplanService;

    /**
     * 课程计划查询
     * @param courseId
     * @return
     */
    @GetMapping("/{courseId}/tree-nodes")
    @Override
    public TeachplanDTO queryTreeNodesByCourseId(@PathVariable("courseId") Long courseId) {
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanDTO teachplanDTO = teachplanService.queryTreeNodesByCourseId(courseId,companyId);
        return teachplanDTO;
    }

    /**
     * 课程计划新增或者修改
     * @param teachplanVO
     * @return
     */
    @PostMapping()
    @Override
    public TeachplanDTO createOrModifyTeachPlan(@RequestBody TeachplanVO teachplanVO) {
        Long companyId = SecurityUtil.getCompanyId();
        TeachplanDTO teachplanDTO = CourseBaseConvert.INSTANCE.teachVO2dto(teachplanVO);
        TeachplanDTO dto = teachplanService.createOrModifyTeachPlan(teachplanDTO,companyId);
        return dto;
    }

    /**
     * 删除课程计划
     * @param teachPlanId
     */
    @DeleteMapping("/{teachplanId}")
    @Override
    public void removeTeachPlan(Long teachPlanId) {
        // 1.获得公司Id
        Long companyId = SecurityUtil.getCompanyId();

        // 2.调用service方法
        teachplanService.removeTeachPlan(teachPlanId,companyId);
    }
}
